package com.sc.weixin.goods;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.sc.dal.common.pagination.PaginationBean;
import com.sc.dal.mapper.goods.GoodsMapper;
import com.sc.dal.model.goods.GoodsClass;
import com.sc.service.goods.GoodsClassService;
import com.sc.service.goods.GoodsService;
import com.sc.util.data.DataUtil;
import com.sc.util.exception.ExceptionEnum;
import com.sc.util.exception.ScBizException;


@RequestMapping(value="goodsClass")
@Controller
public class GoodsClassController {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private GoodsService goodsService;
	@Autowired
	private GoodsClassService goodsClassService;
	
	
	/***
	 * 
	 * 微信端---分类模块数据处理
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/initGoodsClass")
	@ResponseBody
	public Object initGoodsClass(HttpServletRequest request){
		ModelAndView mav = new ModelAndView("/goods/classify");
		try{
			List<GoodsClass> goodsClassList = goodsClassService.selectByGoodsClass();
			mav.addObject("goodsClassList", goodsClassList);
			logger.info("分页查找商品成功！");
		}catch(Exception e){
			logger.error("分页查找商品失败！"+e);
			throw new ScBizException("分页查找商品错误！", e);
		}
		return mav;
	}
	
	/**
	 * 查找商品分类
	 * @return List<GoodsClass>
	 * @param paramMap
	 *  微信端  首页进入列表  分类展示
	 * 
	 */
	@RequestMapping(value="/initIndexByGoodsClass")
	@ResponseBody
	public Object initIndexByGoodsClass(HttpServletRequest request){
		ModelAndView mav = new ModelAndView("/goods/indexClick");
		String id = request.getParameter("id");
		String className = request.getParameter("className");
		List<GoodsClass> goodsClassList = goodsClassService.selectByWxGoodsClass(new Integer(id));
		mav.addObject("goodsClassList", goodsClassList);
		mav.addObject("class_id", id);
		mav.addObject("className", className);
		request.setAttribute("goodsClassList", goodsClassList);
		return mav;
	}
	
	
	/****
	 * 根据ID
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/findIndexByGoodsClass")
	@ResponseBody
	public Object findIndexByGoodsClass(HttpServletRequest request){
		String id = request.getParameter("id");
		Map<String, Object> resultMap = new LinkedHashMap<String,Object>();
		try{
			if(id==null){
				DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
				logger.warn("查找所有商品分类失败！");
				return resultMap;
			}
			List<GoodsClass> goodsClassList = goodsClassService.selectByWxGoodsClass(new Integer(id));
			return goodsClassList;
		}catch (Exception e){
			e.printStackTrace();
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
			logger.warn("查找所有商品分类出错！");
		}
		return resultMap;
	}
	
	
	/****
	 * 微信端首页  5大分类
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/initHomePage")
	@ResponseBody
	public Object initHomePage(HttpServletRequest request){
//		ModelAndView mav = new ModelAndView("/goods/classify");
		Map<String, Object> resultMap = new LinkedHashMap<String,Object>();
		//设置查询参数
		Map<String,Object> paramMap = new HashMap<String,Object>();
		String depth = request.getParameter("depth");
		String pId = request.getParameter("parentId");
		Integer parentId = null;
		String code = request.getParameter("code");
		String status = request.getParameter("status");
		if(StringUtils.isNotBlank(depth)){	//父级层级
			paramMap.put("depth", depth);
		}
		if(StringUtils.isNotBlank(pId)){	//父级分类id
			parentId = Integer.valueOf(pId);
			paramMap.put("parentId", pId);
		}
		if(StringUtils.isNotBlank(code)){	//分类编码
			paramMap.put("code", code);
		}
		if(StringUtils.isNotBlank(status)){	//分类状态，设置app首页的一级分类
			paramMap.put("status", status);
		}
		try{
			List<GoodsClass> goodsClassList = this.goodsClassService.selectList(paramMap);
			if(goodsClassList!=null && goodsClassList.size()>0){
				DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SUCCESS);
				List<Object> classList = DataUtil.getGoodsCateList(goodsClassList, parentId);
				resultMap.put("data", DataUtil.getGoodsCateList(goodsClassList, parentId));
				logger.warn("查找所有商品分类成功！");
			}else{
				DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
				logger.warn("查找所有商品分类失败！");
			}
		}catch (Exception e){
			e.printStackTrace();
			DataUtil.handleResultMap(resultMap, ExceptionEnum.DATA_SELECT);
			logger.warn("查找所有商品分类出错！");
		}
		return resultMap;
		
	}
	
	
	
	
	
	
	@RequestMapping(value="/queryByGoodsClassList")
	@ResponseBody
	public List<GoodsClass> queryByGoodsClassList() {
		List<GoodsClass> GoodsClassList = goodsClassService
				.selectByGoodsClass();
		//request.setAttribute("GoodsClassList", GoodsClassList);
		return GoodsClassList;
		
	}
	
	

}
